﻿
Namespace Controle

    Public Class YCRadioButtonField
        Inherits CheckBoxField 'BoundField

        Private pText As String
        Private pGroupName As String
        Private pValue As String

        'Public Property Text() As String
        '    Get
        '        Return pText
        '    End Get
        '    Set(ByVal value As String)
        '        pText = value
        '    End Set
        'End Property

        Public Property GroupName() As String
            Get
                If pGroupName Is Nothing Then pGroupName = "pGroupName"
                Return pGroupName
            End Get
            Set(ByVal value As String)
                pGroupName = value
            End Set
        End Property

        Public Property Value() As String
            Get
                Return pValue
            End Get
            Set(ByVal value As String)
                pValue = value
            End Set
        End Property



        Public Overrides Sub ExtractValuesFromCell( _
            ByVal dictionary As IOrderedDictionary, _
            ByVal cell As DataControlFieldCell, _
            ByVal rowState As DataControlRowState, _
            ByVal includeReadOnly As Boolean)
            ' Determine whether the cell contain a RadioButton 
            ' in its Controls collection.
            If cell.Controls.Count > 0 Then
                Dim radio As RadioButton = CType(cell.Controls(0), RadioButton)

                Dim checkedValue As Object = Nothing
                If radio Is Nothing Then
                    ' A RadioButton is expected, but a null is encountered.
                    ' Add error handling.
                    Throw New InvalidOperationException( _
                        "RadioButtonField could not extract control.")
                Else
                    checkedValue = radio.Checked
                End If


                ' Add the value of the Checked attribute of the
                ' RadioButton to the dictionary.
                If dictionary.Contains(DataField) Then
                    dictionary(DataField) = checkedValue
                Else
                    dictionary.Add(DataField, checkedValue)
                End If
            End If
        End Sub


        Protected Overrides Sub InitializeDataCell(ByVal cell As System.Web.UI.WebControls.DataControlFieldCell, ByVal rowState As System.Web.UI.WebControls.DataControlRowState)
            MyBase.InitializeDataCell(cell, rowState)

            'Dim radio As New RadioButton()
            Dim radio As New HtmlControls.HtmlGenericControl
            radio.InnerHtml = String.Format("<input type=""radio"" name=""{0}"" value=""{1}"" />", GroupName, Value)

            If DataField.Length <> 0 Then
                AddHandler radio.DataBinding, AddressOf Me.OnDataBindField
            End If

            'radio.Name = GroupName
            'radio.Text = Me.Text
            'radio.GroupName = GroupName


            '' If the row is in edit mode, enable the button.
            If (rowState And DataControlRowState.Edit) <> 0 _
                OrElse (rowState And DataControlRowState.Insert) <> 0 Then
                'radio.Enabled = True
            End If

            cell.Controls.Add(radio)

        End Sub


    End Class

End Namespace